package com.hcss.common.springmvc;

import com.hcss.common.exception.HcssCommonException;
import com.hcss.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class ExceptionHandlerConfig extends ResponseEntityExceptionHandler{
	private static final String COMMON_ERROR_MSG = "系统异常";
	
	@ExceptionHandler(HcssCommonException.class)
	@ResponseBody
	public AjaxResult handleCommonException(HttpServletRequest request, HttpServletResponse response, HcssCommonException ex) {
		logger.error(ex.getMessage(), ex);
		return new AjaxResult(AjaxResult.code_error, ex.getMessage());
	}
	
	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public AjaxResult handleOtherException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
		logger.error(ex.getMessage(), ex);
		return new AjaxResult(AjaxResult.code_error, COMMON_ERROR_MSG);
	}
}
